<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册页面</title>
    <link rel="stylesheet" href="css/conmmon.css">
    <link rel="stylesheet" href="css/login.css">
    <!-- 引入 jquery -->
    <script src="js/jquery.min.js"></script>
    <script src="js/tools.js"></script>
</head>

<body>
    <!-- 导航栏 -->
    <div class="nav">
        <img src="img/logo2.jpg" alt="">
        <span class="title">我的博客系统</span>
        <!-- 用来占据中间位置 -->
        <span class="spacer"></span>
        <a href="blog_list.html">主页</a>
        <a href="login.html">登陆</a>
    </div>
    <!-- 版心 -->
    <div class="login-container">
        <!-- 中间的注册框 -->
        <div class="login-dialog">
            <h3>注册</h3>
            <div class="row">
                <span>用户名</span>
                <input type="text" id="username">
            </div>
            <div class="row">
                <span>密码</span>
                <input type="password" id="password">
            </div>
            <div class="row">
                <span>确认密码</span>
                <input type="password" id="password2">
            </div>
            <div class="kaptcha">
                <span>验证码</span>
                <input type="text" id="rightCode">
                <img alt="验证码" id="imgs" onclick="update()" 
                    src=""/>
            </div>
            <div class="row">
                <button id="submit" input="inputMethod" onclick="mysub()">提交</button>
            </div>
            
        </div>
    </div>
</body>

<script>
    //当前时间戳
    var timeStamp=new Date()*1;

    //更新验证码
    function update(){
        var p=document.getElementById("imgs");
        //在 ajax 请求 url 后面加上一个随机数 new Date()防止使用相同缓存导致数据不更新
        timeStamp=new Date()*1;
        p.src='/admin/defaultKaptcha?d=' +timeStamp;
    }
    //第一次从服务器获取验证码
    update();

    jQuery(function(){
        //键盘按下事件
        window.onkeydown=enter;
    });

    //按下回车键注册
    function enter(){
        if(window.event.keyCode==13){
            jQuery("#submit").click();
        }
    }
    function mysub(){
        //1.参数校验
        var username=jQuery("#username");
        var password=jQuery("#password");
        var password2=jQuery("#password2");
        var rightCode=jQuery("#rightCode");
        if(username.val()==""){
            alert("请输入用户名");
            username.focus();
            return false;
        }
        if(password.val()==""){
            alert("请输入密码");
            password.focus();
            return false;
        }
        if(password2.val()==""){
            alert("请确认密码");
            password2.focus();
            return false;
        }
        if(password.val()!=password2.val()){
            alert("两次密码输入不一致，请先检查！");
            password.focus();
            return false;
        }
        //2.发送请求给后端
        jQuery.ajax({
            url:"user/reg",
            type:"POST",
            data:{
                username:username.val(),
                password:password.val(),
                rightCode:rightCode.val(),
                timeStamp:timeStamp
            },
            success:function(result){
                if(result.code==200&&result.data==1){
                    alert("恭喜，注册成功！");
                    if(confirm("是否跳转至登录页？")){
                        location.href = "login.html";
                    }
                }else if(result.code==200&&result.data==0){
                    alert("抱歉：验证码输入错误，请重新输入！");
                    //刷新验证码
                    var p=document.getElementById("imgs");
                    p.src='/admin/defaultKaptcha?d='+new Date()*1;
                    rightCode.focus();
                }else{
                    alert(result.msg);
                }
            }
        });  
    }
</script>
</html>